<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/31
 * Time: 11:02
 */

namespace app\index\server;


use app\index\model\Agent as AgentModel;
use app\lib\exception\DatabaseException;
use think\Exception;

class AgentServer extends BaseServer
{
    public function getList($params = [])
    {
        // 参数的准备
        $prep_params = $this->prep_param($params);
        try {
            // 总条数
            $count = AgentModel::count();
            $banner_list = AgentModel::order($prep_params['sort'], $prep_params['order'])
                ->limit($prep_params['offset'], $prep_params['limit'])
                ->select();
        } catch (Exception $e) {
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return bootstrap_table_return($count, $banner_list);
    }

    public function detail($id)
    {
        try {
            $info = AgentModel::where('id', $id)
                ->find();
            if(!$info) {
                return setMsg(0, '未找到此信息');
            }
        } catch (Exception $e) {
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return setMsg(1, 'OK', $info);
    }

    public function addOrUpdate($params = [])
    {
        // 检测code是否存在
        $code = $params['invitation_code'];
        $where = [];
        $where['invitation_code'] = ['=', $code];
        if(isset($params['primary_key'])) $where['id'] = ['<>', $params['primary_key']];
        $code_result = AgentModel::where($where)
            ->find();
        if($code_result) {
            return setMsg(0, '非常抱歉，您的code重复');
        }
        $prep_data = [
            'invitation_code' => $code,
            'address' => $params['address'],
            'status' => $params['status'],
            'agent_balance' => $params['agent_balance'],
            'description' => $params['description']
        ];
        try {
            if(isset($params['primary_key'])) {
                // 更新
                $result = AgentModel::where('id', $params['primary_key'])
                    ->update($prep_data);
            } else {
                $result = AgentModel::insert($prep_data);
            }
            if(!$result) {
                return setMsg(0, '代理操作失败');
            }
        } catch (Exception $e) {
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return setMsg(1, '代理操作成功');
    }

    public function changeStatus($id)
    {
        try {
            $info = AgentModel::where('id', $id)
                ->find();
            $info->status = abs(($info->status) - 1);
            $result = $info->save();
            if(!$result) {
                return setMsg(0, '修改此代理状态失败');
            }
        }catch (Exception $e) {
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return setMsg(1, '修改此代理 状态成功');
    }
}